<template>
	<view class="">
		<u-toast ref="uToast" />
		<u-navbar :is-back="true" title="物品管理" back-icon-color='#333333' height="50" :border-bottom="false">
			<view class="" style="margin-left: 600rpx; display: flex;align-content: center;"
				@click="$toLink('../add_equipment/add_equipment',{})">
				<image src="../../static/tupian96.png" mode="widthFix" style="width: 30rpx;"></image>
			</view>
		</u-navbar>
		<view class="top">
			<view class="topBox">
				<view v-for="(topItem,topIndex) in inventory_list" :key="topIndex" :data-name="topIndex"
					@click="changeIndex" :class="[activeIndex==topIndex?'con':'']">
					<text></text>
					{{topItem}}
				</view>
			</view>
		</view>
		<view class="list" v-for="(item,index) in goodsList" :key="index"
			@click="$toLink('../goods_record/goods_record',{id:item.goods_id,name:item.name})">
			<view class="per_goods">
				<view class="top">
					<text>{{item.goods_type}}</text>
					<text>{{item.name}}</text>
					<text>{{item.startTime}}</text>
				</view>
				<view class="center">
					<image src="../../static/tupian114.png" mode="widthFix"></image>
					<text>{{item.teacher_users}}</text>
				</view>
				<view class="bottom">
					<image src="../../static/tupian103.png" mode="widthFix"></image>
					<text>{{item.rooms}}</text>
					<image src="../../static/tupian88.png" mode="widthFix"
						@click.stop="toggle_page(item)">
					</image>
				</view>
			</view>
		</view>
		<u-loadmore :status="status" />
	</view>
</template>
<script>
	import request from '../../request.js'
	import time from '../../time.js'
	export default {
		data() {
			return {
				inventory_list: ['全部', '信息化', '总务', '教学', '其他'],
				goods_list: [],
				activeIndex: 0,
				status: 'loading',
				page: 0,
				last_page: '',
			}
		},
		onLoad(e) {
			this.goodsLists(0);
		},
		onReachBottom() { //滚动到底部触发该函数
			console.log('触发了吗')
			if (this.page >= this.last_page) {
				this.status = 'nomore';
				return
			} else {
				this.status = 'loading';
			} //如果大于最后一页那么就中断函数

			this.page = ++this.page;

			setTimeout(() => {
				this.goodsLists(this.activeIndex, this.page);
			}, 2000)
		},
		computed: {
			goodsList() {
				if (this.goods_list) {
					this.goods_list.forEach(item => {
						this.$set(item, 'startTime', time.format(item.create_time));
					})
					return this.goods_list;
				} else {
					return [];
				}
			}
		},
		methods: {
			changeIndex(e) {
				console.log(e.currentTarget.dataset.name);
				let index = e.currentTarget.dataset.name;
				this.activeIndex = index;
				this.page = 0;
				this.goodsLists(this.activeIndex, 1);
			},
			//跳转到编辑页面传递参数
			toggle_page(param){
				this.$toLink('../edit_equipment/edit_equipment',{name:param.name,type:param.goods_type,rooms:param.rooms,goods_id:param.goods_id,teacher_users:param.teacher_users,full_name:param.user_info?param.user_info.full_name:'',room_list:param.room_ids.join(','),teacher_list:param.teacher_user_ids.join(',')})
			},
			toggle(index) {
				let str = "";
				switch (index) {
					case 0:
						str = -1;
						break;
					case 1:
						str = '信息化';
						break;
					case 2:
						str = '总务';
						break;
					case 3:
						str = '教学';
						break;
					case 4:
						str = '其他';
						break;
				}
				return str;
			},
			goodsLists(param, param2) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let parameter = this.toggle(param);
				let pagination = param2 ? param2 : 1;
				let _this = this;
				request({
					url: base_url + 'teacher/goods/goodsLists',
					method: 'post',
					data: {
						goods_type: parameter,
						page: pagination,
					},
					success(res) {
						if (res.statusCode == 200) {
							console.log(res);
							console.log(res.data.data.data, res.data.data.last_page);
							_this.last_page = res.data.data.last_page;
							if (res.data.data.total == 0) {
								console.log('又触发吗');
								_this.status = 'nomore'
								//如果接口返回的数据为零的话，那么就中断函数
								_this.goods_list.length = 0;
								//没有数据的话那么就将数组清空
								_this.goods_list = [];
								return;
							};
							//页数为1的情况
							if (pagination == 1) {
								//先将原数组清空
								_this.goods_list = [];
								console.log('进来了吗');
								if (res.data.data.data == null) {
									console.log('执行到这了吗')
									_this.goods_list = [];
								} else {
									_this.goods_list = res.data.data.data;
								}
								console.log(_this.goods_list);
								_this.page = 1;
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
								//页数大于1的情况
							} else if (pagination > 1) {
								console.log(res.data.data.data);
								if (res.data.data.data == null) {
									_this.status = 'nomore'
								}
								_this.goods_list = [..._this.goods_list, ...res.data.data.data];
								console.log(_this.goods_list);
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
							}
							console.log(_this.goods_list);

						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.top {
		width: 100%;
		height: 65rpx;
		background: #fff;
		overflow: hidden;

		.topBox {
			width: 100%;
			height: 65rpx;
			background: #fff;
			position: relative;
			display: flex;
			justify-content: space-evenly;
			border-bottom: 1px solid #F2F2F2;

			view {
				height: 65rpx;
				line-height: 65rpx;
				width: 120rpx;
				text-align: center;
				font-size: 30rpx;
				color: #fff;
				color: #333333;

				text {
					display: none;
				}


			}

			.con {
				position: relative;
				height: 65rpx;
				line-height: 65rpx;
				color: #2F96FE;
				width: 120rpx;

				text {
					display: block !important;
					position: absolute;
					width: 120rpx;
					height: 4rpx;
					background: #2F96FE;
					bottom: 0rpx;
					left: 0rpx;
				}
			}

		}
	}

	.list {
		width: 100%;
		height: 203rpx;
		overflow: hidden;

		.per_goods {
			margin-top: 22rpx;
			margin-left: 20rpx;
			height: 179rpx;
			width: 710rpx;
			border-radius: 15rpx;
			box-shadow: 0px 1px 1px 1px #ccc;

			.top {
				width: 100%;
				height: 60rpx;
				display: flex;
				justify-content: flex-start;

				text {
					margin-top: 20rpx;

					&:nth-child(1) {
						background-color: #ABC0F7;
						width: 60rpx;
						margin-left: 20rpx;
						height: 30rpx;
						margin-top: 25rpx;
						overflow: hidden;
						border-radius: 10rpx;
						color: #fff;
						font-size: 16rpx !important;
						text-align: center;
						line-height: 30rpx;
					}

					&:nth-child(2) {
						color: #333333;
						width: 355rpx;
						margin-left: 30rpx;
						font-size: 28rpx;
					}

					&:nth-child(3) {
						margin-left: 30rpx;
						color: #999999;
						font-size: 20rpx;
						margin-top: 25rpx;

					}
				}

				.active {
					background: #EC654E !important;
				}
			}

			.center {
				width: 100%;
				height: 55rpx;
				display: flex;
				justify-content: flex-start;

				image {
					&:nth-child(1) {
						margin-top: 25rpx;
						margin-left: 20rpx;
						width: 30rpx;
					}
				}

				text {
					&:nth-child(2) {
						margin-top: 25rpx;
						margin-left: 30rpx;
						font-size: 24rpx;
						color: #333333;
					}
				}

			}

			.bottom {
				width: 100%;
				height: 55rpx;
				display: flex;
				justify-content: flex-start;

				image {
					&:nth-child(1) {
						margin-top: 15rpx;
						margin-left: 20rpx;
						width: 30rpx;
					}

					&:nth-child(3) {
						margin-top: 15rpx;
						margin-left: 10rpx;
						width: 30rpx;
					}

				}

				text {
					&:nth-child(2) {
						margin-top: 15rpx;
						margin-left: 30rpx;
						width: 560rpx;
						font-size: 24rpx;
						color: #333333;
					}
				}
			}
		}
	}

	.goods_list {
		width: 100%;
		height: 203rpx;
		overflow: hidden;

		.per_goods {
			margin-top: 22rpx;
			margin-left: 20rpx;
			height: 179rpx;
			width: 710rpx;
			border-radius: 12rpx;
			box-shadow: 0px 1px 1px 1px #ccc;

			.top {
				width: 100%;
				height: 60rpx;
				display: flex;
				justify-content: flex-start;

				text {
					margin-top: 20rpx;

					&:nth-child(1) {
						background-color: #ABC0F7;
						width: 60rpx;
						margin-left: 20rpx;
						margin-top: 25rpx;
						height: 30rpx;
						border-radius: 8rpx;
						color: #fff;
						font-size: 16rpx;
						text-align: center;
						line-height: 30rpx;
					}

					&:nth-child(2) {
						color: #333333;
						margin-left: 30rpx;
						width: 280rpx;
						font-size: 28rpx;
					}

					&:nth-child(3) {
						margin-left: 30rpx;
						color: #999999;
						margin-top: 25rpx;
						font-size: 20rpx;

					}

					&:nth-child(4) {
						margin-left: 20rpx;
						color: #999999;
						margin-top: 25rpx;
						font-size: 20rpx;
					}
				}

				.active {
					background: #EC654E !important;
				}
			}

			.center {
				width: 100%;
				height: 55rpx;
				display: flex;
				justify-content: flex-start;

				image {
					&:nth-child(1) {
						margin-top: 25rpx;
						margin-left: 20rpx;
						width: 30rpx;
					}
				}

				text {
					&:nth-child(2) {
						margin-top: 25rpx;
						margin-left: 30rpx;
						font-size: 24rpx;
						color: #333333;
					}
				}

			}

			.bottom {
				width: 100%;
				height: 55rpx;
				display: flex;
				justify-content: flex-start;

				image {
					&:nth-child(1) {
						margin-top: 15rpx;
						margin-left: 20rpx;
						width: 30rpx;
					}

					&:nth-child(3) {
						margin-top: 15rpx;
						margin-left: 20rpx;
						width: 30rpx;
					}
				}

				text {
					&:nth-child(2) {
						width: 520rpx;
						margin-top: 15rpx;
						margin-left: 30rpx;
						font-size: 24rpx;
						color: #333333;
					}
				}
			}
		}
	}
</style>
